/*
 *  Copyright 2011 Yuri Kanivets
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.itboye.ihomebank.wheelview.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 摘要车轮适配器提供适配器的通用功能.
 */
public abstract class AbstractWheelTextAdapter extends AbstractWheelAdapter {
    
    /** 文本视图资源。用作适配器的默认视图. */
    public static final int TEXT_VIEW_ITEM_RESOURCE = -1;
    
    /**没有资源不断 */
    protected static final int NO_RESOURCE = 0;
    
    /** 默认文本颜色 */
    public static final int DEFAULT_TEXT_COLOR = 0xFF585858;
    
    /** 默认文本颜色 */
    public static final int LABEL_COLOR = 0xFF700070;
    
    /** 默认文本大小 */
    public static final int DEFAULT_TEXT_SIZE = 18;
    
    // 文本设置
    private int textColor = DEFAULT_TEXT_COLOR;
    private int textSize = DEFAULT_TEXT_SIZE;
    
    // 当前上下文
    protected Context context;
    // Layout inflater
    protected LayoutInflater inflater;
    
    // 项目资源
    protected int itemResourceId;
    protected int itemTextResourceId;
    
    // 空项目资源
    protected int emptyItemResourceId;
	
    /**
     * 构造函数
     * @param context the current context
     */
    protected AbstractWheelTextAdapter(Context context) {
        this(context, TEXT_VIEW_ITEM_RESOURCE);
    }

    /**
     * Constructor
     * @param 上下文
     * @param 资源ID为一个布局文件包含一个TextView时使用实例化项目视图
     */
    protected AbstractWheelTextAdapter(Context context, int itemResource) {
        this(context, itemResource, NO_RESOURCE);
    }
    
    /**
     * Constructor
     * @param 上下文
     * @param 资源ID为一个布局文件包含一个TextView时使用实例化项目视图
     * @param 资源ID为文本视图中的项目布局
     */
    protected AbstractWheelTextAdapter(Context context, int itemResource, int itemTextResource) {
        this.context = context;
        itemResourceId = itemResource;
        itemTextResourceId = itemTextResource;
        
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    /**
     * 获取文本颜色
     * @return 返回文本颜色
     */
    public int getTextColor() {
        return textColor;
    }
    
    /**
     * 设置文本颜色
     * @param 文字颜色文字颜色设置
     */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }
    
    /**
     * 获取文本大小
     * @return 返回文本大小
     */
    public int getTextSize() {
        return textSize;
    }
    
    /**
     * 设置文字大小
     * @param 文字大小文字大小设置
     */
    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }
    
    /**
     * 获取项目视图的资源标识
     * @return 返回项目资源标识
     */
    public int getItemResource() {
        return itemResourceId;
    }
    
    /**
     * 设置项目视图的资源标识
     * @param 资源ID设置
     */
    public void setItemResource(int itemResourceId) {
        this.itemResourceId = itemResourceId;
    }
    
    /**
     * 获取项目布局中的文本视图的资源标识 
     * @return 返回项目文本资源标识
     */
    public int getItemTextResource() {
        return itemTextResourceId;
    }
    
    /**
     * 在项目布局中设置文本视图的资源标识
     * @param itemTextResourceId 项文本资源ID设置
     */
    public void setItemTextResource(int itemTextResourceId) {
        this.itemTextResourceId = itemTextResourceId;
    }

    /**
     * 获取空项目视图的资源标识
     * @return 返回空项目资源标识
     */
    public int getEmptyItemResource() {
        return emptyItemResourceId;
    }

    /**
     * 设置空项目视图的资源标识
     * @param 空项目资源ID设置
     */
    public void setEmptyItemResource(int emptyItemResourceId) {
        this.emptyItemResourceId = emptyItemResourceId;
    }
    
    
    /**
     * 返回指定项目的文本
     * @param 指标项目指标
     * @return 返回指定项目的文本
     */
    protected abstract CharSequence getItemText(int index);

    @Override
    public View getItem(int index, View convertView, ViewGroup parent) {
        if (index >= 0 && index < getItemsCount()) {
            if (convertView == null) {
                convertView = getView(itemResourceId, parent);
            }
            TextView textView = getTextView(convertView, itemTextResourceId);
            if (textView != null) {
                CharSequence text = getItemText(index);
                if (text == null) {
                    text = "";
                }
                textView.setText(text);
    
                if (itemResourceId == TEXT_VIEW_ITEM_RESOURCE) {
                    configureTextView(textView);
                }
            }
            return convertView;
        }
    	return null;
    }

    @Override
    public View getEmptyItem(View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getView(emptyItemResourceId, parent);
        }
        if (emptyItemResourceId == TEXT_VIEW_ITEM_RESOURCE && convertView instanceof TextView) {
            configureTextView((TextView)convertView);
        }
            
        return convertView;
	}

    /**
     * 配置文本视图
     * @param view 查看文本视图进行配置
     */
    protected void configureTextView(TextView view) {
        view.setTextColor(textColor);
        view.setGravity(Gravity.CENTER);
        view.setTextSize(textSize);
        view.setEllipsize(TextUtils.TruncateAt.END);
        view.setLines(1);
//        view.setCompoundDrawablePadding(20);
//        view.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
    }
    
    /**
     * 从视图加载文本视图
     * @param 查看文本视图或布局包含它
     * @param 文本在布局的资源ID
     * @return 返回加载的文本视图
     */
    public TextView getTextView(View view, int textResource) {
    	TextView text = null;
    	try {
            if (textResource == NO_RESOURCE && view instanceof TextView) {
                text = (TextView) view;
            } else if (textResource != NO_RESOURCE) {
                text = (TextView) view.findViewById(textResource);
            }
        } catch (ClassCastException e) {
            Log.e("AbstractWheelAdapter", "You must supply a resource ID for a TextView");
            throw new IllegalStateException(
                    "AbstractWheelAdapter requires the resource ID to be a TextView", e);
        }
        
        return text;
    }
    
    /**
     * 从资源视图
     * @param 资源ID
     * @return 如果资源不是设置的话，返回加载的视图或空
     */
    public View getView(int resource, ViewGroup parent) {
        switch (resource) {
        case NO_RESOURCE:
            return null;
        case TEXT_VIEW_ITEM_RESOURCE:
            return new TextView(context);
        default:
            return inflater.inflate(resource, parent, false);    
        }
    }
}
